//
//  WaterFlowLayout.m
//  UICollectionDemo_01
//
//  Created by runlhy on 16/6/16.
//  Copyright © 2016年 runlhy. All rights reserved.
//

#import "WaterFlowLayout.h"

const CGFloat defaultColumnMargin = 3;
const CGFloat defaultRowMargin = 3;
UIEdgeInsets defaultEdgeInsets = {3, 3, 3, 3};
NSInteger defaultColumnCount = 1;
//static const NSInteger defaultHeight = ScrollViewHeight;

@interface WaterFlowLayout ()


@end

@implementation WaterFlowLayout

#pragma mark - lazy
- (NSMutableArray *)attrsArray
{
    if (!_attrsArray){
        _attrsArray = [NSMutableArray array];
    }
    return _attrsArray;
}

/**
 *  初始化
 */
- (void)prepareLayout{
    [super prepareLayout];
    self.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 30);
    // 清除之前所有的布局属性
    [self.attrsArray removeAllObjects];
    self.attrsArray = nil;
    
    //创建cell对应的布局属性
    for (int j=0; j<[self.collectionView numberOfSections]; j++) {
        NSInteger count = [self.collectionView numberOfItemsInSection:j];
        for (NSInteger i = 0; i < count; i++) {
            //创建位置
            NSIndexPath *indexpath = [NSIndexPath indexPathForItem:i inSection:j];
            //获取indexPath对应的布局属性
            UICollectionViewLayoutAttributes *attrs = [self layoutAttributesForItemAtIndexPath:indexpath];
            
            [self.attrsArray addObject:attrs];
        }
    }
}
/**
 *  决定cell的排布
 */
- (NSArray<UICollectionViewLayoutAttributes *> *)layoutAttributesForElementsInRect:(CGRect)rect{
    //NSLog(@"%s",__func__);
    return self.attrsArray;
}
/**
 *  返回indexpath对应cell的 布局的属性
 */
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath{
    //创建布局属性
    UICollectionViewLayoutAttributes *attributes = [UICollectionViewLayoutAttributes layoutAttributesForCellWithIndexPath:indexPath];
    switch (indexPath.section) {
        case 0:
        case 2:
            defaultColumnCount = 1;
            defaultEdgeInsets = UIEdgeInsetsMake(0, 0, 5, 0);
            break;
        case 1:
            defaultColumnCount = 4;
            break;
        case 3:
            defaultEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
            defaultColumnCount = 2;
            break;
        default:
            break;
    }
    
    // collectionView的宽度
    CGFloat collectionViewW = self.collectionView.frame.size.width;
    CGFloat w = (collectionViewW - defaultEdgeInsets.left - defaultEdgeInsets.right - (defaultColumnCount - 1) * defaultColumnMargin) / defaultColumnCount;
    //CGFloat h = w *  1.5;//indexPath.section <= 3 ? 0.5 :
    CGFloat h = 130.0f;// = indexPath.section == 3 ? w * 1.5 : 130;//130;//indexPath.section <= 3 ? 0.5 :
    CGFloat x = defaultEdgeInsets.left;
    CGFloat y = defaultEdgeInsets.top;
    
    switch (indexPath.section) {
        case 1:
            h = IconViewHeight;
            break;
        case 3:
            h = w * 1.5;
            break;
            
        default:
            break;
    }
    
    //根据计算位置
    NSInteger section = indexPath.item / defaultColumnCount;
    NSInteger row = indexPath.item % defaultColumnCount;
    y = section * ((h + defaultRowMargin)) + defaultEdgeInsets.top;
    x = defaultEdgeInsets.left + row * (w + defaultColumnMargin);
    
    //计算每一组的高度
    y += _sectionHeight;
    //设置每一个cell的位置信息
    attributes.frame = CGRectMake(x, y , w, h);
    
    //每一组的最大高度的计算
    _maxSectionHeight = y + h;
    //更新高度  - -  collectionview为啥整体向下偏移上一次collectionView的最大高度??????
    if (indexPath.item == [self.collectionView numberOfItemsInSection:indexPath.section] - 1) {
        //SNSLog(@" indexPath.section = %ld , indexPath.item = %ld",indexPath.section ,indexPath.item);
        //组高度 = 每一组最大Y值 + 每一组中cell的底部偏移量
        _sectionHeight = _maxSectionHeight + defaultEdgeInsets.bottom;
    }
    //获得最底部位置 - collectionView的课滚动范围
    _maxHeight = _maxSectionHeight + defaultEdgeInsets.bottom;
    return attributes;
    
}

/** 滚动范围 */
- (CGSize)collectionViewContentSize {
    return CGSizeMake(0,_maxHeight);
}


@end
